<?php
    require_once "lang/" . $array_config['language'] . ".php";
    require_once "core/sql_controller.php";

    Class AppController{

        public $Lang;
        public $SQL;
        public $Config;
        public $Vendors;

        function __construct($array_config=null)
        {
            $this->Config = $array_config;
            $this->Lang = new Lang();
            $this->SQL = new SQLController();
        }

        function create_user_class_array($className, $params = array())
        {
            $reflection = new ReflectionClass($className);
            return $reflection->newInstanceArgs($params);
        }

        public function View($view,$Layout)
        {
            if (sizeof($_SESSION['SET_VARS']) > 0)
            {
                foreach ($_SESSION['SET_VARS'] as $var=>$value)
                {
                    if (is_array($value))
                    {
                        eval( "$".$var . "= array();" );
                        eval( "$".$var . ' = $value;' );
                    }else{
                        eval( "$".$var . "='" . $value . "';" );
                    }

                }
            }

            if ($PageTitle == ""){$PageTitle = ucwords($view);}
            
            if (file_exists("views/" . strtolower($view) . ".php"))
            {
                if ($Layout == ''){$Layout='default';}
                include "views/layouts/$Layout.php";
            }else{
                //echo "Error cargando la vista " . $view;
                header("HTTP/1.0 404 Not Found");
            }
        }

        public function Element($element)
        {
            if (file_exists("views/elements/" . strtolower($element) . ".php"))
            {
                include "views/elements/" . strtolower($element) . ".php";
            }else{
                echo "Error cargando el elemento " . $element;
            }
        }

        public function Helper($helper)
        {
            if (file_exists("views/helpers/" . strtolower($helper) . ".php"))
            {
                include "views/helpers/" . strtolower($helper) . ".php";
            }else{
                echo "Error cargando el helper" . $helper;
            }
        }


        public function Vendor($vendor)
        {
            if (file_exists("vendors/". $vendor . "/" . strtolower($vendor) . ".php"))
            {
                include "vendors/" . $vendor . "/" . strtolower($vendor) . ".php";
                @eval('$this->Vendors->'.ucwords($vendor) .' = new '.ucwords($vendor).'Vendor();');
            }else{
                die("Vendor " . $vendor . " error");
            }
        }

        public function set($var,$value)
        {
            $_SESSION['SET_VARS'][$var] = $value;
        }

        public function debug($var)
        {
            echo "<pre>";
            print_r($var);
            echo "</pre>";
        }
    }
?>